package com.grupoconstruobras.sigo.core.context;

import java.security.Principal;
import java.util.logging.Logger;

import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.ejb.EJBContext;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.transaction.UserTransaction;

/*@Default
@SessionScoped*/
@Stateless(name = "sessionContext", mappedName = "context/sessionContext")
@EJB(beanName = "sessionContext", beanInterface = ISessionContext.class, name = "ejb/context/sessionContext")
public class SessionContextImpl implements ISessionContext {
	
	private static Logger LOG = Logger.getLogger(SessionContextImpl.class.getName());
	
	//@Resource
	private EJBContext ejbContext;
	//@Resource
	private UserTransaction transaction;
	@Resource
	private Principal principal;
	
	@Inject
	private HttpSession session;
	@Inject
	private ServletContext context;
	@Inject
	private HttpServletRequest req;
	
	@Override
	public Principal getPrincipal() {
		return ((HttpServletRequest) req).getUserPrincipal();
	}
	
	@Override
	public String getIpAddress() {
		String ipAddress = req.getHeader("X-FORWARDED-FOR");  
	   if (ipAddress == null || ipAddress.isEmpty()) {  
		   ipAddress = req.getRemoteAddr();  
	   }
	   return ipAddress;
	}

	@Override
	public boolean isUserInRole(String role) {
		return req.isUserInRole(role);
	}
	
}
